package com.societyGames.outputExercises
{
import com.societyGames.action.DelayAction;
import com.societyGames.codeExercise.CodeExercise;

import flash.events.TextEvent;

public class OutputCodeExercise extends CodeExercise
{
  public var outputSimulation:OutputSimulation;

  override public function initialize():void
  {
    this.outputSimulation.initialize(this._actionQueue);
    this.addChild(this.outputSimulation.sprite);

    this.outputEventObject.addEventListener(onQueuedOutput, false, 0, true);
  }

  public override function queueOutput(...arguments):void
  {
    super.queueOutput.apply(this, arguments);

    var text:String = createOutputText(arguments);
    outputSimulation.addOutputText(text);
    this._actionQueue.add(new DelayAction(250));
  }

  //Delayed output through action queue.
  private function onQueuedOutput(event:TextEvent):void
  {
    outputSimulation.renderOutputText(event.text);
  }

  protected override function evaluateWithExceptionsHandled():void
  {
    outputSimulation.render.showFirstError();
    if (this.outputSimulation.doesTextMatch())
    {
      this.reportSuccess();
    }
    else
    {
      this.reportFailure();
    }
  }
}
}
